﻿Imports System
Imports System.Web.UI

Namespace [Shared].UserControls
    Public Class DropDownListBaseControl
        Inherits UserControl

#Region "Fields"

        Private _labelText As String

#End Region

#Region "Properties"

        Property LabelText As String
            Get
                Return _labelText
            End Get
            Set(value As String)
                _labelText = value

                If String.IsNullOrEmpty(_labelText) Then
                    LabelDiv.Attributes.Add("style", "display: none;")
                End If
            End Set
        End Property

        Property AutoPostBack As Boolean
            Get
                Return ControlDropDownList.AutoPostBack
            End Get
            Set(value As Boolean)
                ControlDropDownList.AutoPostBack = value
            End Set
        End Property

        Property SelectedControlValue As String
            Get
                Return ControlDropDownList.SelectedValue
            End Get
            Set(value As String)
                ControlDropDownList.SelectedValue = value
            End Set
        End Property

        WriteOnly Property CssClass As String
            Set(ByVal value As String)
                ControlDropDownList.CssClass = value
            End Set
        End Property

        WriteOnly Property PanelCssClass As String
            Set(value As String)
                ControlPanel.CssClass = String.Format("input {0}", value).Trim()
            End Set
        End Property

        WriteOnly Property IsReadOnly As Boolean
            Set(value As Boolean)
                Dim css As String = ControlDropDownList.CssClass

                If String.IsNullOrEmpty(css) Then
                    css = "ddl"
                End If

                If value AndAlso (Not css.Contains("readonly")) Then
                    css = css.Replace("req", String.Empty)
                    css = String.Format("{0} readonly", css)
                ElseIf Not value AndAlso (Not css.Contains("req")) Then
                    css = css.Replace("readonly", String.Empty)
                    css = String.Format("{0} req", css)
                End If

                ControlDropDownList.CssClass = css
                ControlDropDownList.Enabled = Not value
            End Set
        End Property

        WriteOnly Property DataSource As IEnumerable(Of KeyValuePair(Of String, String))
            Set(value As IEnumerable(Of KeyValuePair(Of String, String)))
                For Each entity As KeyValuePair(Of String, String) In value
                    ControlDropDownList.Items.Add(New ListItem(entity.Value, entity.Key))
                Next entity
            End Set
        End Property

#End Region
        
#Region "Events"

        Event SelectedIndexChanged(sender As Object, e As EventArgs)

        Private Sub ControlSelectedIndexChanged(sender As Object, e As EventArgs) Handles ControlDropDownList.SelectedIndexChanged
            RaiseEvent SelectedIndexChanged(sender, e)
        End Sub

#End Region

    End Class
End Namespace
